package com.ztl.multicamera;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;
import androidx.core.app.NavUtils;

import android.content.Context;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Handler;
import android.text.Layout;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dc.ztllib.BuildConfig;

import java.text.SimpleDateFormat;
import java.util.Date;
import ZtlApi.ZtlManager;

public class MainActivity extends AppCompatActivity {

    public enum MessageType {
        msgOpenCam,
        msgCloseCam,
    }

    Thread autoThread;
    boolean is_running = false;

    boolean isBarOpenst = false;
    CameraUIHelper cameraUIHelper;

    TextView logo = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_main);

        if (BuildConfig.DEBUG) Log.e("MainActivity", "onCreate");

        ZtlManager.GetInstance().setContext( getApplicationContext() );
        isBarOpenst = ZtlManager.GetInstance().isSystemBarOpen();
        ZtlManager.GetInstance().openSystemBar(false );

        logo = findViewById(R.id.logo);

        cameraUIHelper = new CameraUIHelper();
        cameraUIHelper.init(this );

        String udiskPath = ZtlManager.GetInstance().getUsbStoragePath();
        Log.e("udisk","path" + udiskPath);
        //为了最佳的显示效果。目前是手工指定。避免自动计算导致的效果不和谐的问题。
        //因为：比如2个摄像头，在横屏和竖屏的情况下，界面分布是不一样的。横屏，一排2列。 竖屏则上下分列显示就可以了。

        /*autoAC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                is_running = !is_running;
                if( is_running ){
                    autoAC.setText("停止测试");
                    txtlog.setVisibility(View.VISIBLE);
                    //执行自动测试的线程
                    autoThread = new Thread(new Runnable() {
                        @Override
                        public void run() {
                            while (true) {
                                if (is_running) {
                                    mHandler.sendEmptyMessage(MessageType.msgOpenCam.ordinal());
                                }

                                try {
                                    Thread.sleep(4000);
                                } catch (InterruptedException e) {
                                    e.printStackTrace();
                                    break;
                                }

                                if( is_running ){
                                    try {
                                        Thread.sleep(4000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                        break;
                                    }

                                    mHandler.sendEmptyMessage(MessageType.msgCloseCam.ordinal());

                                    try {
                                        Thread.sleep(4000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                        break;
                                    }
                                }
                            }
                        }
                    });

                    autoThread.start();


                }else{
                    autoAC.setText("自动测试");
                    if(autoThread != null)
                    {
                        autoThread.interrupt();
                        autoThread = null;
                    }
                    txtlog.setVisibility(View.INVISIBLE);
                }
            }
        });*/


    }

    @Override
    protected void onStop() {
        super.onStop();

        cameraUIHelper.closeCameras();

        if( autoThread != null){
            autoThread.interrupt();
            autoThread = null;
        }

        //恢复成原样
        ZtlManager.GetInstance().openSystemBar( isBarOpenst );
    }

    @Override
    protected void onResume() {
        super.onResume();

        cameraUIHelper.openCameras();

        logo.bringToFront();
    }

    void openCam() {


    }

    int nTestTime = 1;
    public Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            MessageType mt = MessageType.values()[msg.what];
            switch (mt) {
                case msgOpenCam: {

                    ///outputLog("开始第" + nTestTime + "次测试");
                }
                break;
                case msgCloseCam: {

                   // outputLog("结束第" + nTestTime + "次测试");
                    nTestTime++;
                }
                break;
            }
        }
    };

}
